set -x
CURRENT_PATH=$(dirname $(readlink -f $0))
echo "begin to get stat..."
if [[ ! -d "${CURRENT_PATH}/stat" ]];then
    mkdir ${CURRENT_PATH}/stat
else
    echo 1
    # echo delete old stat
    # rm -rf ${CURRENT_PATH}/stat/*
fi
i=1

since_year_month=$1
since_year=${since_year_month:0:4}
since_month=${since_year_month:5:2}
since_month=${since_month#0}

until_year_month=$2
until_year=${until_year_month:0:4}
until_month=${until_year_month:5:2}
until_month=${until_month#0}

for ((i=since_year;i<=until_year;i++))
do
    if [ $i -eq $since_year ]; then
        j=$since_month
    else
        j=1
    fi
    for((;j<=12;j++))
    do

        if [ $j -lt 10 ]; then
            month="0$j"
        else
            month="$j"
        fi
        if [[ $j -gt $until_month ]] && [[ $i -eq $until_year ]]; then
            break
        fi

        year_month="$i-$month"
        if [[ ! -d "${CURRENT_PATH}/stat/${year_month}" ]];then
            mkdir ${CURRENT_PATH}/stat/${year_month}
        else
            rm -rf ${CURRENT_PATH}/stat/${year_month}/*
        fi

        python3 cantian_code_statistic.py ${year_month}
        if [[ $? -ne 0 ]]; then
            echo get stat failed
            exit 1
        fi
        python3 mysql_connector_code_statistic.py ${year_month}
        if [[ $? -ne 0 ]]; then
            echo get stat failed
            exit 1
        fi
        python3 get_cantian_pr_comment.py ${year_month}
        if [[ $? -ne 0 ]]; then
            echo get stat failed
            exit 1
        fi
        python3 get_mysql_connector_pr_comment.py ${year_month}
        if [[ $? -ne 0 ]]; then
            echo get stat failed
            exit 1
        fi
        python3 merge.py ${year_month}
        if [[ $? -ne 0 ]]; then
            echo get stat failed
            exit 1
        fi

    done
done

echo "get stat complete!"
set +x